﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;

namespace HP.HierarchyWindow
{
    public class UIHierarchy : IVsUIHierarchy
    {
        private uint lastCookie;
        private ImageList imageList;
        private Dictionary<uint, IVsHierarchyEvents> eventSinks;

        public UIHierarchy()
        {
            eventSinks = new Dictionary<uint, IVsHierarchyEvents>();
            imageList = ResourceUtil.LoadImageListStream(this.GetType(), "HP.HierarchyWindow.treeicons.bmp", new Size(16, 16), true, new Point(0, 0));
        }

        public int AdviseHierarchyEvents(IVsHierarchyEvents pEventSink, out uint pdwCookie)
        {
            eventSinks.Add(++lastCookie, pEventSink);

            pdwCookie = lastCookie;

            return 0;
        }

        public int Close()
        {
            return 0;
        }

        public int ExecCommand(uint itemid, ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (pguidCmdGroup == typeof(VSConstants.VsUIHierarchyWindowCmdIds).GUID)
            {
                var cmd = (VSConstants.VsUIHierarchyWindowCmdIds)nCmdID;
            }

            return 0;
        }

        public int GetCanonicalName(uint itemid, out string pbstrName)
        {
            pbstrName = string.Empty;

            return 0;
        }

        public int GetGuidProperty(uint itemid, int propid, out Guid pguid)
        {
            var item = (VSConstants.VSITEMID)itemid;
            var property1 = (__VSHPROPID)propid;
            var property2 = (__VSHPROPID2)propid;
            var property3 = (__VSHPROPID3)propid;
            var property4 = (__VSHPROPID4)propid;

            pguid = Guid.Empty;

            return VSConstants.DISP_E_MEMBERNOTFOUND;
        }

        public int GetNestedHierarchy(uint itemid, ref Guid iidHierarchyNested, out IntPtr ppHierarchyNested, out uint pitemidNested)
        {
            ppHierarchyNested = IntPtr.Zero;
            pitemidNested = 0;

            return VSConstants.E_FAIL;
        }

        public int GetProperty(uint itemid, int propid, out object pvar)
        {
            var item = (VSConstants.VSITEMID)itemid;
            var property1 = (__VSHPROPID)propid;
            var property2 = (__VSHPROPID2)propid;
            var property3 = (__VSHPROPID3)propid;
            var property4 = (__VSHPROPID4)propid;

            if (property1 == __VSHPROPID.VSHPROPID_IconImgList)
            {
                pvar = imageList.Handle;
                return 0;
            }
            else if (property1 == __VSHPROPID.VSHPROPID_Expandable || property1 == __VSHPROPID.VSHPROPID_ExpandByDefault)
            {
                pvar = 1;
                return 0;
            }
            else if (property1 ==  __VSHPROPID.VSHPROPID_Caption)
            {
                pvar = "Hello";
                return 0;
            }
            else if (property1 == __VSHPROPID.VSHPROPID_IconIndex || property1 == __VSHPROPID.VSHPROPID_OpenFolderIconIndex || property1 == __VSHPROPID.VSHPROPID_StateIconIndex || property1 == __VSHPROPID.VSHPROPID_OverlayIconIndex)
            {
                if (item == VSConstants.VSITEMID.Root)
                {
                    pvar = 0;
                    return 0;
                }
                else
                {
                    Debugger.Break();
                }
            }
            else if (property1 == __VSHPROPID.VSHPROPID_FirstChild || property1 == __VSHPROPID.VSHPROPID_FirstVisibleChild)
            {
                pvar = VSConstants.VSITEMID_NIL;
                return VSConstants.DISP_E_MEMBERNOTFOUND;
            }
            else if (property2 == __VSHPROPID2.VSHPROPID_NoDefaultNestedHierSorting)
            {
                pvar = 1;
                return 0;
            }
            else if (property2 == __VSHPROPID2.VSHPROPID_StatusBarClientText)
            {
                pvar = "Hello from Ken's personal hierarchy window.";
                return 0;
            }
            else if (property2 == __VSHPROPID2.VSHPROPID_AppTitleBarTopHierarchyName)
            {
                pvar = "Ken's Hierarchy Window.";
                return 0;
            }
            else if (property3 == __VSHPROPID3.VSHPROPID_ProductBrandName)
            {
                pvar = "Ken's Hierachies Inc.";
                return 0;
            }
            else if (property1 == __VSHPROPID.VSHPROPID_ItemDocCookie)
            {
                pvar = 22;
                return 0;
            }
            else
            {
                pvar = null;
                return VSConstants.DISP_E_MEMBERNOTFOUND;
            }

            pvar = null;
            return VSConstants.DISP_E_MEMBERNOTFOUND;
        }

        public int GetSite(out Microsoft.VisualStudio.OLE.Interop.IServiceProvider ppSP)
        {
            ppSP = null;

            return 0;
        }

        public int ParseCanonicalName(string pszName, out uint pitemid)
        {
            pitemid = 0;

            return 0;
        }

        public int QueryClose(out int pfCanClose)
        {
            pfCanClose = 1;

            return 0;
        }

        public int QueryStatusCommand(uint itemid, ref Guid pguidCmdGroup, uint cCmds, Microsoft.VisualStudio.OLE.Interop.OLECMD[] prgCmds, IntPtr pCmdText)
        {
            return 0;
        }

        public int SetGuidProperty(uint itemid, int propid, ref Guid rguid)
        {
            var item = (VSConstants.VSITEMID)itemid;
            var property1 = (__VSHPROPID)propid;
            var property2 = (__VSHPROPID2)propid;
            var property3 = (__VSHPROPID3)propid;
            var property4 = (__VSHPROPID4)propid;

            return 0;
        }

        public int SetProperty(uint itemid, int propid, object var)
        {
            var item = (VSConstants.VSITEMID)itemid;
            var property1 = (__VSHPROPID)propid;
            var property2 = (__VSHPROPID2)propid;
            var property3 = (__VSHPROPID3)propid;
            var property4 = (__VSHPROPID4)propid;

            return 0;
        }

        public int SetSite(Microsoft.VisualStudio.OLE.Interop.IServiceProvider psp)
        {
            return 0;
        }

        public int UnadviseHierarchyEvents(uint dwCookie)
        {
            return 0;
        }

        public int Unused0()
        {
            return 0;
        }

        public int Unused1()
        {
            return 0;
        }

        public int Unused2()
        {
            return 0;
        }

        public int Unused3()
        {
            return 0;
        }

        public int Unused4()
        {
            return 0;
        }
    }
}
